package cn.hfatec.shms.system.codeType.service;

import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.shms.advice.annotation.MysqlDataSource;
import cn.hfatec.shms.enums.CodeType;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * @author wzr
 * @version 1.0
 *          <p/>
 *          the JAVA code is generate by middlegen
 */
@Service("CodeTypeService")
public class CodeTypeService {

    /**
     * 查询数据
     *
     * @param page
     */
    @Transactional
    @MysqlDataSource
    public Table queryCodeTypeTable(String codeTypeId, String codeTypeName, Page page) {

        CodeType[] list = CodeType.values();
        //总条数
        int totalSize = list.length;
        //总页数
        int totalPage = totalSize % page.getPageSize() == 0 ? totalSize / page.getPageSize() : (totalSize / page.getPageSize() + 1);
        page.setTotalSize(totalSize);
        page.setTotalPage(totalPage);
        List<CodeType> codeTypeList = new ArrayList();
        int number;//循环到i<number时结束
        if (totalSize < (page.getCurrentPage() * page.getPageSize())) {
            number = totalSize;
        } else {
            number = page.getCurrentPage() * page.getPageSize();
        }
        if (StringUtils.isNotBlank(codeTypeId) || StringUtils.isNotBlank(codeTypeName)) {
            for (int i = 0; i < totalSize; i++) {
                if ((StringUtils.isNotBlank(codeTypeName) && list[i].getDesc().indexOf(codeTypeName) != -1) ||
                        (StringUtils.isNotBlank(codeTypeId) && list[i].name().indexOf(codeTypeId) != -1)) {
                    codeTypeList.add(list[i]);
                }
            }
        } else {
            for (int i = (page.getCurrentPage() * page.getPageSize()) - page.getPageSize(); i < number; i++) {
                codeTypeList.add(list[i]);
            }
        }
        Table table = new Table();
        table.setPage(page);
        table.setRows(codeTypeList);
        return table;
    }

}